package com.freesky.web.manager.controller;

import com.freesky.common.dao.entity.Role;
import com.freesky.common.dao.entity.User;
import com.freesky.common.enums.ResponseEnum;
import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.web.manager.service.UserService;
import com.freesky.web.manager.shiro.CurrentUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author Freesky
 */
@Controller
@Api(description = "管理员管理")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @ApiOperation(value = "进入页面-用户列表")
    @GetMapping("/user/page")
    public String userPage() {
        return "user/list";
    }

    @ApiOperation(value = "进入页面-用户添加")
    @GetMapping("/user/add/page")
    public String userAddPage() {
        return "user/add";
    }

    @ApiOperation(value = "进入页面-用户修改")
    @GetMapping("/user/update/page")
    public String userUpdatePage() {
        return "user/update";
    }
    @ApiOperation(value = "进入页面-用户修改密码")
    @GetMapping("/user/change-password/page")
    public String userChangePasswordPage() {
        return "user/change-password";
    }

    @ApiOperation(value = "进入页面-用户信息查看")
    @GetMapping("/user/info/page")
    public String userInfoPage(Model model) {
        User user = CurrentUser.INSTANCE.getUser();
        log.info("当前用户{}",user);
        model.addAttribute("user", user);
        return "user/info";
    }

    @ApiOperation(value = "进入页面-角色列表")
    @GetMapping("/user/role/page")
    public String rolePage() {
        return "user/role/list";
    }

    @ApiOperation(value = "进入页面-角色添加")
    @GetMapping("/user/role/add/page")
    public String roleAddPage() {
        return "user/role/add";
    }

    @ApiOperation(value = "进入页面-角色修改")
    @GetMapping("/user/role/update/page")
    public String roleUpdatePage() {
        return "user/role/update";
    }

    @ResponseBody
    @GetMapping(value = "/user/info")
    @ApiOperation(value = "获取登录用户信息")
    public JSONResponse getUserInfo() {
        User user = CurrentUser.INSTANCE.getUser();
        user.setPassword(null);
        return JSONResponseDiretor.buildSuccessJSONResponse(user);
    }

    @ResponseBody
    @GetMapping(value = "/user/role/list")
    @ApiOperation(value = "获取角色列表")
    public DataTablesResult getRoleList() {
        return userService.getRoleList();
    }

    @ResponseBody
    @GetMapping(value = "/user/role/all")
    @ApiOperation(value = "获取所有角色")
    public JSONResponse getAllRoles() {
        return userService.getAllRoles();
    }

    @ResponseBody
    @GetMapping(value = "/user/role/exist-name")
    @ApiOperation(value = "判断角色是否已存在")
    public boolean roleName(String name) {

        if (userService.getRoleByRoleName(name) != null) {
            return false;
        }
        return true;
    }

    @ResponseBody
    @GetMapping(value = "/user/role/edit/exist-name/{id}")
    @ApiOperation(value = "判断编辑角色是否已存在")
    public boolean roleName(@PathVariable int id, String name) {

        return userService.getRoleByEditName(id, name);
    }

    @ResponseBody
    @PostMapping(value = "/user/role/add")
    @ApiOperation(value = "添加角色")
    public JSONResponse addRole(@ModelAttribute Role role) {
        return userService.addRole(role);
    }

    @ResponseBody
    @PostMapping(value = "/user/role/update")
    @ApiOperation(value = "更新角色")
    public JSONResponse updateRole(@ModelAttribute Role role) {
        return userService.updateRole(role);
    }

    @ResponseBody
    @DeleteMapping(value = "/user/role/del/{ids}")
    @ApiOperation(value = "删除角色")
    public JSONResponse delRole(@PathVariable int[] ids) {
        for (int id : ids) {
            int result = userService.deleteRole(id);
            if (result == ResponseEnum.ROLE_IN_USE.getCode()) {
                return JSONResponseDiretor.buildErrorJSONResponse("id为" + id + "的角色被使用中，不能删除！");
            }
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @ResponseBody
    @GetMapping(value = "/user/role/count")
    @ApiOperation(value = "统计角色数")
    public JSONResponse getRoleCount() {
        return userService.countRole();
    }

    @ResponseBody
    @GetMapping(value = "/user/perm/list")
    @ApiOperation(value = "获取权限列表")
    public JSONResponse getPermissionList() {
        return userService.getPermissionList();
    }

    @ResponseBody
    @GetMapping(value = "/user/perm/count")
    @ApiOperation(value = "统计权限数")
    public JSONResponse getPermissionCount() {
        return userService.countPermission();
    }
    @ResponseBody
    @GetMapping(value = "/user/perm/role-id")
    @ApiOperation(value = "根据角色id获取对应权限")
    public JSONResponse getPermNameListByRoleId(@RequestParam(value = "roleId") Integer roleId) {
        return userService.getPermsByRoleId(roleId);
    }




    @ResponseBody
    @GetMapping(value = "/user/list")
    @ApiOperation(value = "获取用户列表")
    public DataTablesResult getUserList() {
        return userService.getUserList();
    }

    @ResponseBody
    @RequestMapping(value = "/user/exist/username")
    @ApiOperation(value = "判断用户名是否存在")
    public boolean getUserByName(String username) {
        return userService.getUserByName(username);
    }

    @ResponseBody
    @GetMapping(value = "/user/exist/phone")
    @ApiOperation(value = "判断手机是否存在")
    public boolean getUserByPhone(String phone) {
        return userService.getUserByPhone(phone);
    }

    @ResponseBody
    @GetMapping(value = "/user/exist/email")
    @ApiOperation(value = "判断邮箱是否存在")
    public boolean getUserByEmail(String email) {

        return userService.getUserByEmail(email);
    }

    @ResponseBody
    @PostMapping(value = "/user/add")
    @ApiOperation(value = "添加用户")
    public JSONResponse addUser(@ModelAttribute User user) {
        return userService.addUser(user);
    }

    @ResponseBody
    @PostMapping(value = "/user/update")
    @ApiOperation(value = "更新用户")
    public JSONResponse updateUser(@ModelAttribute User user) {
        return userService.updateUser(user);
    }

    @ResponseBody
    @GetMapping(value = "/user/edit/exist/username/{id}")
    @ApiOperation(value = "判断编辑用户名是否存在")
    public boolean getUserByEditName(@PathVariable Long id, String username) {
        return userService.getUserByEditName(id, username);
    }

    @ResponseBody
    @GetMapping(value = "/user/edit/exist/phone/{id}")
    @ApiOperation(value = "判断编辑手机是否存在")
    public boolean getUserByEditPhone(@PathVariable Long id, String phone) {
        return userService.getUserByEditPhone(id, phone);
    }

    @ResponseBody
    @GetMapping(value = "/user/edit/exist/email/{id}")
    @ApiOperation(value = "判断编辑用户名是否存在")
    public boolean getUserByEditEmail(@PathVariable Long id, String email) {

        return userService.getUserByEditEmail(id, email);
    }
    @ResponseBody
    @PutMapping(value = "/user/stop/{id}")
    @ApiOperation(value = "停用用户")
    public JSONResponse stopUser(@PathVariable Long id) {
        return userService.changeUserState(id, 0);
    }
    @ResponseBody
    @PutMapping(value = "/user/start/{id}")
    @ApiOperation(value = "启用用户")
    public JSONResponse startUser(@PathVariable Long id) {
        return userService.changeUserState(id, 1);
    }
    @ResponseBody
    @PostMapping(value = "/user/change-password")
    @ApiOperation(value = "修改用户密码")
    public JSONResponse changePass(@ModelAttribute User user) {
        return userService.changePassword(user);
    }
    @ResponseBody
    @GetMapping(value = "/user/del/{ids}")
    @ApiOperation(value = "删除用户")
    public JSONResponse delUser(@PathVariable Long[] ids) {

        for (Long id : ids) {
            userService.deleteUser(id);
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }
    @ResponseBody
    @GetMapping(value = "/user/count")
    @ApiOperation(value = "统计用户数")
    public JSONResponse getUserCount() {
        return userService.countUser();
    }
}
